<template>
  <!-- 品名信息维护 -->
  <custom-container ref="customContainerRef" :config="mainConfig" />
  <setForm ref="setFormRef" @changeList="handleQuery" />
</template>
<script lang="ts" setup>
import { Delete, Refresh, Plus, Download, Edit, Document } from '@element-plus/icons-vue'
import { ref, getCurrentInstance, defineAsyncComponent, computed } from 'vue'
import { ElMessage, ElMessageBox, ElButton } from 'element-plus'
import { DictDirection } from '@/store/modules/dicts'
import { getData, addData, editData, PutData, delData, Fanyi, delOrderCategoryImg } from '@/api/BusinessConfig/articleNamemaintain'

const setForm = defineAsyncComponent(() => import('./components/setForm.vue'))
let customContainerRef = ref(null),
  ids = [],
  // 非单个禁用
  single = ref(true),
  // 非多个禁用
  multiple = ref(true),
  // 日期范围
  dateRange = ref([]),
  // 查询参数
  queryParams = ref({
    pageNum: 1,
    pageSize: 10,
    roleName: undefined,
    roleKey: undefined,
    status: undefined,
  }),
  setFormRef = ref(null)

// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1
  customContainerRef.value.getList()
}
// 列表选择
const handleSelectionChange = (rows: { [key: string]: any }[]) => {
  ids = rows.map((item) => item.id)
  // custCodes = rows.map((item) => item.custCode)
  single.value = rows.length != 1
  multiple.value = !rows.length
}
// 新增品名
const handleAdd = () => {
  setFormRef.value.open = true
  setFormRef.value.title = '新增品名信息'
  setFormRef.value.getMenuTreeselect()
}
// 修改品名
const handleUpdate = (row: { [key: string]: any }) => {
  setFormRef.value.open = true
  const id = row?.id || ids
  setFormRef.value.title = '修改品名信息'
  setFormRef.value.editData(id)
}
// 删除品名
const handleDelete = (row: { [key: string]: any }) => {
  const id = row.id || ids
  console.log(row.roleId)
  ElMessageBox.confirm(`是否确认删除品名编号为"${ id }"的数据项?`, '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
  .then(() => delData(id))
  .then((res) => {
    if (res.code === 200) {
      console.log('ss')
      handleQuery()
      ElMessage.success('删除成功')
    } else {
      ElMessage.error(res.msg)
    }
  })
  .catch((error) => {
    if (error == 'cancel') {
      ElMessage.error(error)
    }
  })
}
// 主体配置
const mainConfig = computed({
  get() {
    return {
      query: {
        fields: [
          {
            label: 'HS编码',
            prop: 'hsCode',
          },
          {
            label: '品名中文名称',
            prop: 'productName',
            width: 150,
          },
        ],
        queryParams: queryParams.value,
        formOption: {
          labelWidth: '0.65rem',
        },
      },
      btns: [
        {
          title: '新增',
          icon: Plus,
          directive: [['hasPermi', ['system:role:add']]],
          onClick: handleAdd,
        },
        {
          title: '删除',
          icon: Delete,
          type: 'danger',
          disabled: multiple.value,
          directive: [['hasPermi', ['system:role:remove']]],
          onClick: handleDelete,
        },
      ],
      table: {
        columns: [
          { type: 'selection', width: '55', align: 'center' },
          {
            label: 'HS编码',
            prop: 'hsCode',
            align: 'center',
            width: '90',
          },
          {
            label: '品名中文名称',
            prop: 'productName',
            align: 'center',
            width: 150,
          },
          {
            label: '品名英文名称',
            prop: 'productNameEn',
            align: 'center',
            width: 150,
          },
          {
            label: '材质',
            prop: 'material',
            align: 'center',
            width: 150,
          },
          {
            label: '材质英文名称',
            prop: 'materialEn',
            align: 'center',
            width: 150,
          },
          {
            label: '用途',
            prop: 'categoryUse',
            align: 'center',
            width: 150,
          },
          {
            label: '用途英文名称',
            prop: 'categoryUseEn',
            align: 'center',
            width: 150,
          },
          {
            label: '生产商',
            prop: 'manufacturer',
            align: 'center',
            width: 150,
          },
          {
            label: '生产商英文名称',
            prop: 'manufacturerEn',
            align: 'center',
            width: 150,
          },
          {
            label: '原产地',
            prop: 'placeOrigin',
            align: 'center',
            width: 150,
          },
          {
            label: '原产地英文名称',
            prop: 'placeOriginEn',
            align: 'center',
            width: 150,
          },
          {
            label: '操作',
            align: 'center',
            width: 150,
            showOverflowTooltip: false,
            render: (h, params) => {
              const buttons = [
                {
                  title: '修改',
                  icon: Edit,
                  clickFun: handleUpdate,
                },
                {
                  title: '删除',
                  icon: Delete,
                  clickFun: handleDelete,
                },
              ]
              return buttons.map((el) => h(
                ElButton,
                {
                  text: true,
                  type: 'primary',
                  icon: el.icon,
                  onClick: () => {
                    el.clickFun(params.row)
                  },
                },
                {
                  default: () => el.title,
                },
              ))
            },
          },
        ],
        event: {
          onSelectionChange: (rows) => {
            handleSelectionChange(rows)
          },
        },
      },
      api: getData,
    }
  },
  set(val) {
    return val
  },
})
</script>
